<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <template #header>
          <el-row type="flex" justify="space-between">
            <span>权限管理</span>
            <el-button type="primary" @click="addPerm(1, '0')">
              添加权限
            </el-button>
          </el-row>
        </template>
        <!-- card body -->
        <el-table border :data="tableData" row-key="id">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <!-- 权限类型：type=1 页面访问权限 type=2页面下操作功能权限（没有子，不能添加） -->
              <!-- 2. 添加的是页面下功能权限（type=2） -->
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="addPerm(2, row.id)"
              >
                添加
              </el-button>
              <el-button type="text" @click="editPerm(row.id)">编辑</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog
      title="添加权限"
      :visible.sync="showAddPer"
      width="50%"
      @close="closePop"
    >
      <el-form
        ref="form"
        :model="formData"
        :rules="rules"
        label-width="80px"
        size="normal"
      >
        <!-- 权限名称 -->
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <!-- 权限标识 -->
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <!-- 权限描述 -->
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <!-- 权限启用 -->
        <el-form-item label="权限启用" size="normal">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            active-text="启用"
            inactive-value="0"
            inactive-text="不启用"
          />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showAddPer = false">取消</el-button>
        <el-button type="primary" @click="submitAdd">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { toTreeData } from '@/utils'
import { addPermission, getPermissionDetail } from '@/api/permission'
export default {
  data () {
    return {
      showAddPer: false,
      tableData: [],
      formData: {
        enVisible: '1', //  权限是否启用 0-不启用/1-启用
        name: '', // 权限点名称
        code: '', // 权限标识（身份标识=》权限控制就用它判断）
        description: '', // 权限描述
        type: '', // 权限类型 1-页面访问 2-页面下功能
        pid: '' // 添加到哪个节点下 =》	权限点父节点ID（'0'代表是页面权限 | '604f7df5f900be1850edb152'代表页面下功能权限）
      },
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限点标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getPerList()
  },
  methods: {
    async editPerm (id) {
      console.log('编辑')
      try {
        const res = await getPermissionDetail(id)
        this.showAddPer = true
        this.formData = res
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async submitAdd () {
      try {
        await this.$refs.form.validate()
        // if (this.formData.id) {

        // } else {
        const res = await addPermission(this.formData)
        console.log(res)
        // }

        this.showAddPer = false
        this.getPerList()
      } catch (error) {
        console.log(error)
      }
    },
    addPerm (type, id) {
      this.showAddPer = true
      this.formData.type = type
      this.formData.pid = id
    },
    async getPerList () {
      try {
        const res = await getPermissionList()
        this.tableData = toTreeData(res)
      } catch (error) {
        console.log(error)
      }
    },
    closePop () {
      this.showAddPer = false
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
